<?php
/* ----------------------------------------------------------
--               JacoCMS by Jaco Ruit                      --
-------------------------------------------------------------
--     JacoCMS is Open Source and is lincenced under       --
--             GNU General Public Licence v3               --
--          http://www.gnu.org/copyleft/gpl.html           --
-------------------------------------------------------------
--       Please don't remove any text from the footers.    --
-------------------------------------------------------------
-- Credits:   * Jaco Ruit          ~     Scripts,MySQL     --       
-----------------------------------------------------------*/

# JacoCMS Page Class

class Page
{
	public function getByName($nametosearch)
	{
		$query = "SELECT id, content, keywords, title FROM pages WHERE title LIKE'" . $nametosearch . "'";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $row['id'];
			$title = $row['title'];
			$content = $row['content'];
			$keywords = $row['keywords'];
		}
		$arr = array("id" => $id, "title" => $title, "content" => $content, "keywords" => $keywords);
		mysql_free_result($result);
		return $arr;
	}
	
	public function getByID($idtosearch)
	{
		$query = "SELECT content, keywords, title FROM pages WHERE id='" . $idtosearch . "'";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$title = $row['title'];
			$content = $row['content'];
			$keywords = $row['keywords'];
		}
		$arr = array("id" => $idtosearch, "title" => $title, "content" => $content, "keywords" => $keywords);
		mysql_free_result($result);
		return $arr;
	}
	
	public function getHTMLCode($array)
	{
		return '<br />
				<div id="pagecontent">' . $array['content'] . '</div><br />
			
				<!-- AddThis Button BEGIN -->
					<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
						<a class="addthis_button_preferred_1"></a>
						<a class="addthis_button_preferred_2"></a>
						<a class="addthis_button_preferred_3"></a>
						<a class="addthis_button_preferred_4"></a>
						<a class="addthis_button_compact"></a>
						<a class="addthis_counter addthis_bubble_style"></a>
					</div>	<div id="articlefooter">
					<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4e2f390741f02fb9"></script>
					<!-- AddThis Button END --></div>'; 
	}
	
	public function getMetaTags($array)
	{
		return '<meta name="description" content="' . strip_tags($array['content']) . '" />
				<meta name="keywords" content="' . $array['keywords'] . '" />
				<meta name="generator" content="JacoCMS" />';
	}
	
	public function getMenuCode()
	{
		$menucode = "";
		$query = "SELECT title FROM pages";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$title = $row['title'];
			$menucode .= '<li><a href="p-' . urlencode(strtolower($title)) . '">' . $title . '</a></li>';
		}
		mysql_free_result($result);
		return $menucode;
	}
	
	public function getLastID()
	{
		$query = "SELECT id FROM pages ORDER BY id DESC";
		$result = mysql_query($query);
		$lastid = null;
		$lastid = mysql_fetch_array($result);
		mysql_free_result($result);
		return $lastid['id'];
	}
	
	public function checkIfExists($nametosearch)
	{
		$query = "SELECT title FROM pages WHERE title='" . $nametosearch . "'";
		$result = mysql_query($query);
		if (mysql_num_rows($result) == 1)
		{
			mysql_free_result($result);
			return true;
		}
		else
		{
			mysql_free_result($result);
			return false;
		}
	}
	
	public function checkIfIDExists($idtosearch)
	{
		$query = "SELECT title FROM pages WHERE id='" . $idtosearch . "'";
		$result = mysql_query($query);
		if (mysql_num_rows($result) == 1)
		{
			mysql_free_result($result);
			return true;
		}
		else
		{
			mysql_free_result($result);
			return false;
		}
	}
	
	public function rowCount()
	{
		$query = "SELECT title FROM pages";
		$result = mysql_query($query);
		$count = mysql_num_rows($result);
		return $count;
	}
	
	public function update ($idtoup, $title, $content, $keywords)
	{
		$query = "UPDATE pages SET title = '" . $title . "', content = '" . $content . "', keywords =  '" . $keywords . "' WHERE id='" . $idtoup . "'";
		$result = mysql_query($query);
	}
	
	public function add ($title, $content, $keywords)
	{
		$newid = $this->getLastID() + 1;
		$query = "INSERT INTO pages (id,title,content,keywords) VALUES ('" . $newid . "','" . $title . "','" . $content . "','" . $keywords . "')";
		$result = mysql_query($query);
	}
	
	public function delete($idtodel)
	{
		$query = "DELETE FROM pages WHERE id='" . $idtodel . "'";
		$result = mysql_query($query);
	}
	
	public function getTable($array)
	{
		return '<tr><td>' . $array['id'] . '</td><td>' . $array['title'] . '</td><td><a href="deletep-' . $array['id'] . '">Delete</a> <a href="editpage-' . $array['id'] . '">Edit</a></td></tr>';
	}
	
	
	
	public function getWholeTable()
	{
		$htmlcode = "<table><tr><th>ID</th><th>Title</th></tr>";
		$query = "SELECT title, id FROM pages ORDER BY id DESC";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $row['id'];
			$title = $row ['title'];
			$arr = array("id" => $id, "title" => $title);
			$htmlcode .= $this->getTable($arr);
		}
		$htmlcode .= '</table>';
		mysql_free_result($result);
		return $htmlcode;
	}
}

?>